package hn.cch.netty;

import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLException;
import java.io.File;

public class NettyContext {

    private static Logger logger = LoggerFactory.getLogger(NettyContext.class);

    private String crtFile;
    private String keyFile;
    private String pemFile;


    public NettyContext(String crtFile, String keyFile, String pemFile) {
        this.crtFile = crtFile;
        this.keyFile = keyFile;
        this.pemFile = pemFile;
    }


    public SslContext getSslContext(){
        SslContext sslContext = null;
        try {
            sslContext = SslContextBuilder.forServer(new File(crtFile), new File(keyFile))
                    .trustManager(new File(pemFile)).clientAuth(ClientAuth.REQUIRE)
                    .build();
        } catch (SSLException e) {
            logger.error("ssl error:", e.getMessage());
        }
        return sslContext;
    }



}
